import org.newdawn.slick.Animation;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.tiled.TiledMap;


/**
 * 
 * movement based on location on grid
 *
 */
public class Game2 extends BasicGame {
	//private float playerX = 320;
	//private float playerY = 240;
	private TiledMap map;
	private Animation player;
	private int playerX = 0;
	private int playerY = 0;

	public Game2() {
		super("My Test");
	}

	public void init(GameContainer container) throws SlickException {
		container.setVSync(true);
		SpriteSheet sheet = new SpriteSheet("images/karbonator.png", 32, 32);
		map = new TiledMap("images/simplelevel.tmx");
		player = new Animation();
		player.setAutoUpdate(true);
		for (int frame = 0; frame < 3; frame++) {
			player.addFrame(sheet.getSprite(frame, 0), 150);
		}
	}

	public void update(GameContainer container, int delta) {
		Input input = container.getInput();
		int mouseX = input.getMouseX();
		int mouseY = input.getMouseY();
		boolean mousePressed = false;

		if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && mousePressed == false) {
			mousePressed = true;
		}

		if (mousePressed == true) {

			int targetX = mouseX;
			int targetY = mouseY;
			//Int divide, multiply by 16 to get the square we want
			playerX = (targetX/16) * 16; 
			playerY = (targetY/16) * 16;
			
		}
		/*
		if (container.getInput().isKeyDown(Input.KEY_LEFT)) {
			playerX -= 16;
		}
		if (container.getInput().isKeyDown(Input.KEY_RIGHT)) {
			playerX += 16;
		}
		if (container.getInput().isKeyDown(Input.KEY_UP)) {
			playerY -= 16;
		}
		if (container.getInput().isKeyDown(Input.KEY_DOWN)) {
			playerY += 16;
		}
		*/
	}

	public void render(GameContainer container, Graphics g) {
		map.render(0, 0);
		g.drawAnimation(player, playerX, playerY);
	}

	public static void main(String[] argv) throws SlickException {
		AppGameContainer container = new AppGameContainer(new Game2(), 640, 480,
				false);
		container.setTargetFrameRate(60);
		container.start();
	}
}
/*
//Find a path between you and the pixel coordinate target "target" 
public void setTarget(Vector2f target) 
     { 
         this.target = target; 
         fitTargettoGrid(); 
         findPath(); 
         currentStep=0; 
     } 

//Convert pixel coordinates of "target" to grid coordinates. 
public void fitTargettoGrid() 
     { 
    int currentTileX=(int) (target.x/Game.currentMap.getTileWidth()); 
      int currentTileY=(int) (target.y/Game.currentMap.getTileHeight()); 
      target=new Vector2f(currentTileX,currentTileY); 
     } 

//Fit your own coordinates to grid coordinates. 
public void findGridPosition() 
     { 
           int currentTileX=(int) (parent.getMoveSystem().pos.x/Game.currentMap.getTileWidth()); 
      int currentTileY=(int) (parent.getMoveSystem().pos.y/Game.currentMap.getTileHeight()); 
      gridPosition=new Vector2f(currentTileX,currentTileY); 
     } 

//Find a path from you to the target. 
public void findPath() 
     {  findGridPosition(); 
    pathtoTarget=pathfinder.findPath((Mover)this, (int)gridPosition.getX(), (int)gridPosition.getY(), (int)target.getX(), (int)target.getY()); 
    if(pathtoTarget==null) 
    { 
        System.out.println("ITS NULL"); 
    } 
     } */